package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.google.gson.Gson;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.exceptions.HyphenateException;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.bean.ChatCard;
import com.joplus.housekeeper.network.RequestHelper;
import com.joplus.housekeeper.network.TextCallback;

import com.joplus.housekeeper.utils.L;
import com.joplus.housekeeper.utils.SPUtils;

import org.json.JSONObject;

import java.util.List;


/**
 * 普通卡片订单
 */
public class EaseChatRowText extends EaseChatRow {

    private TextView contentView;

    private LinearLayout layoutCard, layoutCardList;
    private TextView tvCardSubject, tvCardPay;

    private LinearLayout layoutTip;
    private LinearLayout layoutFeed;
    private ImageView ivUnS;
    private ImageView ivS;
    private TextView tvUnS;
    private TextView tvS;
    private TextView tvF;
    private LinearLayout llUnS;
    private LinearLayout lls;

    private String rewarId;
    private String isClick;
    private ImageView gif;


    public EaseChatRowText(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);

    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_message : R.layout.ease_row_sent_message, this);


    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);
        //card
        layoutCard = (LinearLayout) findViewById(R.id.layout_card);
        layoutCardList = (LinearLayout) findViewById(R.id.layout_card_list);
        tvCardSubject = (TextView) findViewById(R.id.tv_card_subject);
        tvCardPay = (TextView) findViewById(R.id.tv_card_pay);
        //TIP
        layoutTip = (LinearLayout) findViewById(R.id.layout_tip);
        ivUnS = (ImageView) findViewById(R.id.tip_iv_unsatisfaction);
        ivS = (ImageView) findViewById(R.id.tip_iv_satisfaction);
        tvUnS = (TextView) findViewById(R.id.tip_tv_unsatisfaction);
        tvS = (TextView) findViewById(R.id.tip_tv_satisfaction);
        llUnS = (LinearLayout) findViewById(R.id.tip_ll_unsatisfaction);
        lls = (LinearLayout) findViewById(R.id.tip_ll_satisfaction);


    }

    @Override
    public void onSetUpView() {
        /*if (message.getBody()!=null){*/
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
        contentView.setText(span, BufferType.SPANNABLE);


        String msg = txtBody.getMessage();
//        if(msg.contains("&quot;")){
//            msg.replace("&quot;","\"\"");
//        }
        try {
            new JSONObject(msg);
            Gson gson = new Gson();
            ChatCard chatCard = gson.fromJson(txtBody.getMessage(), ChatCard.class);
            bubbleLayout.setVisibility(View.GONE);
            layoutCardList.removeAllViews();
            contentView.setVisibility(View.GONE);
            String subject = chatCard.getSubject();
            Log.e(TAG, "onSetUpView: " + chatCard.toString());
            if (subject.equals("reward")) {
                layoutCard.setVisibility(View.GONE);
                layoutTip.setVisibility(VISIBLE);
                setTip(chatCard);
            } else {
                layoutCard.setVisibility(View.VISIBLE);
                layoutTip.setVisibility(GONE);
                setCard(chatCard);
            }
        } catch (Exception e) {
            Log.e(TAG, "onSetUpView: " + e.toString() + txtBody.getMessage());
            bubbleLayout.setVisibility(View.VISIBLE);
            layoutCard.setVisibility(View.GONE);
            layoutTip.setVisibility(View.GONE);
            contentView.setVisibility(View.VISIBLE);
        }
        try {
            handleTextMessage();//不知道什么鬼，里面的processbar竟然出现空指针！
        } catch (Exception e) {

        }


    }

    private void setCard(ChatCard chatCard) {
        tvCardSubject.setText(chatCard.getSubject());

        List<ChatCard.CardInfoBean> cardInfoBeanList = chatCard.getCardInfo();

        for (ChatCard.CardInfoBean cardInfoBean :
                cardInfoBeanList) {


            View view = LayoutInflater.from(context).inflate(R.layout.item_card_list, null);
            TextView tvKey = (TextView) view.findViewById(R.id.tv_card_list_key);
            TextView tvValue = (TextView) view.findViewById(R.id.tv_card_list_value);

            tvKey.setText(cardInfoBean.getKey());
            tvValue.setText(cardInfoBean.getValue());

            layoutCardList.addView(view);


        }

        try {
            JSONObject jsonObject = message.getJSONObjectAttribute("orderStatus");
            setOrderStatus(jsonObject.toString(), chatCard);
            orderStatus(chatCard);
            L.e("本地有该条订单状态: " + jsonObject.toString());
        } catch (HyphenateException e) {
            e.printStackTrace();
            L.e("本地无该条订单状态: " + e.toString());
            orderStatus(chatCard);
        }

    }

    private void setTip(final ChatCard chatCard) {
//        message.getStringAttribute("isClick");

//        try {
//            isClick = message.getStringAttribute("isClick");
//            isClick=(String) SPUtils.get(context,message.getMsgId(),"Id");
//            L.d("HyphenateException","isClick"+isClick+"");
//            if (isClick.equals("s")) {
//                ivUnS.setImageResource(R.drawable.unsatisfaction_n);
//                tvUnS.setTextColor(Color.parseColor("#cccccc"));
//                ivS.setImageResource(R.drawable.satisfaction_s);
//                tvS.setTextColor(Color.parseColor("#000000"));
//            } else if (isClick.equals("unS")) {
//                ivUnS.setImageResource(R.drawable.unsatisfaction_c);
//                tvUnS.setTextColor(Color.parseColor("#000000"));
//                ivS.setImageResource(R.drawable.satisfaction_n);
//                tvS.setTextColor(Color.parseColor("#cccccc"));
//            }else{
//                ivUnS.setImageResource(R.drawable.unsatisfaction_n);
//                tvUnS.setTextColor(Color.parseColor("#cccccc"));
//                ivS.setImageResource(R.drawable.satisfaction_n);
//                tvS.setTextColor(Color.parseColor("#cccccc"));
//            }
//        } catch (HyphenateException e) {
//            e.printStackTrace();
//            L.d("HyphenateException",e.toString());
//
//            isClick=(String) SPUtils.get(context,message.getMsgId(),"Id");
//            L.d("HyphenateException",isClick);
//            ivUnS.setImageResource(R.drawable.unsatisfaction_n);
//            tvUnS.setTextColor(Color.parseColor("#cccccc"));
//            ivS.setImageResource(R.drawable.satisfaction_n);
//            tvS.setTextColor(Color.parseColor("#cccccc"));
//        }
        isClick = (String) SPUtils.get(context, message.getMsgId(), "Id");
        if (isClick.equals("s")) {
            ivUnS.setImageResource(R.drawable.unsatisfaction_n);
            tvUnS.setTextColor(Color.parseColor("#cccccc"));
            ivS.setImageResource(R.drawable.satisfaction_s);
            tvS.setTextColor(Color.parseColor("#000000"));
        } else if (isClick.equals("unS")) {
            ivUnS.setImageResource(R.drawable.unsatisfaction_c);
            tvUnS.setTextColor(Color.parseColor("#000000"));
            ivS.setImageResource(R.drawable.satisfaction_n);
            tvS.setTextColor(Color.parseColor("#cccccc"));
        } else {
            ivUnS.setImageResource(R.drawable.unsatisfaction_n);
            tvUnS.setTextColor(Color.parseColor("#cccccc"));
            ivS.setImageResource(R.drawable.satisfaction_n);
            tvS.setTextColor(Color.parseColor("#cccccc"));
        }

        llUnS.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isClick.equals("unS") && !isClick.equals("s")) {
                    ivUnS.setImageResource(R.drawable.unsatisfaction_c);
                    tvUnS.setTextColor(Color.parseColor("#000000"));
                    ivS.setImageResource(R.drawable.satisfaction_n);
                    tvS.setTextColor(Color.parseColor("#cccccc"));
                    rewarId = chatCard.getRewardId();

                    RequestHelper.getInstance().setSatisfaction("2", rewarId, "  ", new TextCallback() {
                        @Override
                        public void onSuccess(Object info) {

                            message.setAttribute("isClick", "unS");
                            SPUtils.put(context, message.getMsgId(), "unS");
                            updateView();
                        }

                        @Override
                        public void onFailure(String error) {

                        }
                    });


                }

            }
        });
        lls.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                if (!isClick.equals("unS") && !isClick.equals("s")) {
                    RequestHelper.getInstance().setSatisfaction("1", rewarId, "  ", new TextCallback() {
                        @Override
                        public void onSuccess(Object info) {
                            ivUnS.setImageResource(R.drawable.unsatisfaction_n);
                            tvUnS.setTextColor(Color.parseColor("#cccccc"));
                            ivS.setImageResource(R.drawable.satisfaction_s);
                            tvS.setTextColor(Color.parseColor("#000000"));

                            message.setAttribute("isClick", "s");
                            SPUtils.put(context, message.getMsgId(), "s");
                            updateView();
                        }

                        @Override
                        public void onFailure(String error) {

                        }
                    });


                }

            }

        });


    }


    private void orderStatus(final ChatCard chatCard) {

    }

    private void setOrderStatus(String info, final ChatCard chatCard) {
        String txtStatus = "";

        JSONObject jsonObject = null;





            tvCardPay.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {




                }
            });

    }


    protected void handleTextMessage() {
      /*  if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    // 发送消息
                    //               sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }*/
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        L.e("点击了聊天文字");
    }

    private boolean isMyCard(String cardaccoundId) {

        return true;
    }

}
